package leetcode.L500;

import java.util.ArrayList;
import java.util.List;

public class Ex264 {
    public int nthUglyNumber(int n) {
        List<Integer> list = new ArrayList<>();
        int i = 0,j = 0,k = 0;
        list.add(1);

        for (int l = 1; l < n; l++) {
            int a = list.get(i)*2;
            int b = list.get(j)*3;
            int c = list.get(k)*5;
            int next = Math.min(a,Math.min(b,c));
            if(next==a){
                i++;
            }
            if(next==b){
                j++;
            }
            if(next==c){
                k++;
            }
            list.add(next);
        }
        return list.get(n-1);
    }
}
